{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1aac00e7-4136-4127-a40c-b448b88d08dd",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T12:24:13.361494Z",
     "start_time": "2025-01-04T12:24:13.357307Z"
    }
   },
   "source": [
    "<Numpy数组学习手册 numpy基础>\n"
   ]
  },
  {
   "cell_type": "code",
   "id": "4dfa9503-f5c0-4b50-b763-6c3e4f3c4618",
   "metadata": {
    "jupyter": {
     "is_executing": true
    }
   },
   "source": [
    "import numpy as np"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "76744971-bf0b-4e27-8b80-e6b6f1325902",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T11:55:46.363586Z",
     "start_time": "2025-01-04T11:55:46.359741Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "a=np.arange(6)\n",
    "b=np.array([[1,2,3],[10,20,30]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "45ccda67-f608-4f13-b759-c488bf900169",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T11:55:46.394201Z",
     "start_time": "2025-01-04T11:55:46.388343Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5], dtype=int64)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.argsort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3e3edec6-3b49-492a-b665-e981a087268d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 索引和切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1650335d-5fdd-4aa3-a875-61b42a751c94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\na=np.array([0, 1, 2, 3, 4])\\na[0:2]\\na[1:]\\na[-2]\\na[::-1]\\n'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "a=np.array([0, 1, 2, 3, 4])\n",
    "a[0:2]\n",
    "a[1:]\n",
    "a[-2]\n",
    "a[::-1]\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3514514e-156d-470f-923f-47f132170331",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2,  3],\n",
       "       [20, 30]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[:,1:]  # 对行和列切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "35494210-1acc-4ada-b191-aa2055e3bf31",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 3, 30])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[:,2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5e844c0f-5d25-423a-98a4-cedaf827b463",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([ True, False,  True, False,  True, False])]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[a%2==0] # 判断"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "679db682-e6e6-4d3b-bf78-ec401d6e5130",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 2, 4])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[a%2==0] # 根据判断结果筛选 如:a[a>0] 最终会形成一维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c82f5a62-b961-4ebe-8cae-8a54380f0a21",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T11:55:46.456960Z",
     "start_time": "2025-01-04T11:55:46.452707Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int32')"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.dtype # 类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "9178c7a2-cced-491c-b9d1-4baa4ad3623a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T11:55:46.519235Z",
     "start_time": "2025-01-04T11:55:46.514575Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m=np.array([[1,2,3],[4,5,6]])\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "80368345-4d9a-49aa-8147-af97992da038",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T11:55:46.533802Z",
     "start_time": "2025-01-04T11:55:46.529573Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 4, 5, 6])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m[m>2] #筛选结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d8e47418-766f-482c-872b-fb5d98e3eff5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[False, False,  True],\n",
       "       [ True,  True,  True]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m>2 # 判断条件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "5fa4b175-62be-4b2d-a3c4-323fcf234e29",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([], dtype=int64), array([], dtype=int64))"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i=np.nonzero(m<0)\n",
    "i # i是tuple类型 保存满足条件的数据位置,如果没有就返回空tuple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "0844aed6-ea5c-45c5-bbdb-654ea3513f2a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T11:55:46.775697Z",
     "start_time": "2025-01-04T11:55:46.770592Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m[:,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "160bea1a-1c2a-4cba-8494-0b87b847a7ce",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T11:55:47.390523Z",
     "start_time": "2025-01-04T11:55:47.384525Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 7,  9, 11],\n",
       "       [13, 14, 15]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n=np.array([[6,7,8],[9,9,9]])\n",
    "m+n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "1e2afb85-ec6e-4f0a-b0be-6fa7e34c6808",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T11:55:47.535742Z",
     "start_time": "2025-01-04T11:55:47.528880Z"
    },
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-5, -5, -5],\n",
       "       [-5, -4, -3]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m-n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f13db353-fd05-4fd1-8de6-1ba74c9e4a23",
   "metadata": {},
   "source": [
    "## 数组形状和维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "59b80bfe-bf64-4afe-b4fb-14264c1531e1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T11:55:48.351913Z",
     "start_time": "2025-01-04T11:55:48.346792Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 4, 5, 6]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m.reshape(1,6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "af52d5b2-61cc-41b7-a3ce-71fcbe884cc3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T11:55:48.609128Z",
     "start_time": "2025-01-04T11:55:48.603941Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [2],\n",
       "       [3],\n",
       "       [4],\n",
       "       [5],\n",
       "       [6]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m.reshape(6,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "47e9fdfb-5390-4572-a8c1-3a962df923a5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T11:55:48.921377Z",
     "start_time": "2025-01-04T11:55:48.912357Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "e302ef31-3340-4882-adb9-bb929fab4728",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m.ndim # 数组维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "b9519f89-6ef9-4b8c-abb1-b6a7336d769b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[1 1 1]]\n",
      "\n",
      " [[1 1 1]]] (2, 1, 3)\n"
     ]
    }
   ],
   "source": [
    "a= np.ones((2,1,3),np.dtype(int))\n",
    "print(a,a.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "58309eeb-6ca2-40cb-aa20-d44b3a4d386a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T11:55:49.851597Z",
     "start_time": "2025-01-04T11:55:49.846556Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m.reshape(3,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "26e07be1-8ff2-492f-ada7-4437958882b3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T11:55:50.619747Z",
     "start_time": "2025-01-04T11:55:50.609666Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6],\n",
       "       [7],\n",
       "       [8],\n",
       "       [9],\n",
       "       [9],\n",
       "       [9]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n.reshape(6,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "6622c89f-4f5b-49ba-a02c-c2b4181a5c45",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4,  5],\n",
       "       [ 6,  7,  8,  9, 10, 11]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(12).reshape(2,6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "b4d14d21-0982-4d66-9022-f3784607409b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[[1, 1, 1]]],\n",
       "\n",
       "\n",
       "       [[[1, 1, 1]]]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2=np.expand_dims(a,axis=1) # axis=1 给水平方向每一个元素增加维度\n",
    "a2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "c5445523-4af1-4ff5-9498-4ea52f402e24",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[[1, 1, 1]],\n",
       "\n",
       "        [[1, 1, 1]]]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.expand_dims(a,axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2f163a5-1143-42c7-aedb-45d78dc6476b",
   "metadata": {},
   "source": [
    "## 数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "525f2083-5367-45b6-aa86-2f7a17dc3bc4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T11:04:49.794998Z",
     "start_time": "2025-01-04T11:04:49.792719Z"
    }
   },
   "outputs": [],
   "source": [
    "a1=float(1)\n",
    "a2=np.array([1,1,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "ad192774-63b5-4193-9cc9-cc9a1405e7cf",
   "metadata": {
    "jupyter": {
     "is_executing": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 7,  8,  9],\n",
       "       [10, 10, 10]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2+n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "78a278ea-0601-42c6-93cf-6e7d845c3ee2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T10:44:30.608936Z",
     "start_time": "2025-01-04T10:44:30.602757Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 2, 2])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "05e295b2-8ab9-4566-926b-8b6634dc7e5a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T10:44:30.815032Z",
     "start_time": "2025-01-04T10:44:30.808981Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "int(a1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "76037416-1c7a-415b-9fa2-b4b007a9741f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T10:44:30.926794Z",
     "start_time": "2025-01-04T10:44:30.921995Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "80924c1c-ef16-43fc-8f0a-9d222c7fd576",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T10:44:31.031271Z",
     "start_time": "2025-01-04T10:44:31.028381Z"
    }
   },
   "outputs": [],
   "source": [
    "x=np.dtype(float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "9b8ccf08-97ab-4748-a847-53549feddd5d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T10:44:31.090223Z",
     "start_time": "2025-01-04T10:44:31.076102Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'d'"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.char"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "a18587f9-0843-43f8-8759-cbb4c09ba480",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T10:44:31.216472Z",
     "start_time": "2025-01-04T10:44:31.203170Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.float64"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.type"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb37e408-f6e3-4620-b862-49495a44b932",
   "metadata": {},
   "source": [
    "获取dtype类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "b1055f91-2349-4c56-8adf-f785f17d3c29",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T10:44:31.306063Z",
     "start_time": "2025-01-04T10:44:31.298340Z"
    }
   },
   "outputs": [],
   "source": [
    "t=np.dtype([('name',np.str_,40),('numitems',np.int32),('price',np.float32)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "ce30d7c3-fd16-494e-afb7-341fde418c77",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T10:44:31.341182Z",
     "start_time": "2025-01-04T10:44:31.337864Z"
    }
   },
   "outputs": [],
   "source": [
    "p1=np.array([('pen',19,5.8),('water',20,2.0)],dtype=t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "68214154-1775-485b-b257-0f1c2998f369",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T10:44:31.355579Z",
     "start_time": "2025-01-04T10:44:31.349758Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('water', 20, 2.)\n"
     ]
    }
   ],
   "source": [
    "print(p1[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "adfb3d5e-5ae4-482b-91cb-75b150013138",
   "metadata": {},
   "source": [
    "## 新建数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "4c7ccfd8-1675-4596-afd4-8da229a04ab5",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# array(),arange(), ones(),zeros(),full(),Linspace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "b760ff96-928c-41ea-828c-c6ef82a4924e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 3, 4, 5]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([2,3,4,5],ndmin=2)  # ndmin最小维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "24610cce-6851-45ba-a7a3-3a35275e2bbe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 5, 7])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(3,9,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "9aaef37e-f3b5-49f7-a379-90fb33521541",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False, False, False, False, False, False, False,\n",
       "       False])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros(10,dtype=bool)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "2f66dc74-3961-4f76-86fe-3f2df46c1268",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0]])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros([4,5],dtype=int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "75a543c8-8994-4c7c-b78c-027c65e872bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1, 1, 1, 1],\n",
       "       [1, 1, 1, 1, 1, 1]])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.full((2,6),1) #创建一个单独值的n维数组 numpy.full(shape, fill_value, dtype=None)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "46485099-0a71-4e13-8b33-38d5f1ef3eb0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 2, 2, 2, 2]])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.full((1,5),2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "1678bd05-950b-4e0d-be91-8a7b721f8e21",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1. ,  2.8,  4.6,  6.4,  8.2, 10. ])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(1,10,6)  # 创建均分值数组, start,end,number 左闭右闭 默认float "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "0837718e-31c5-4d44-8700-c56d6fe8e9d2",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  1,  2,  3,  4],\n",
       "        [ 5,  6,  7,  8,  9],\n",
       "        [10, 11, 12, 13, 14],\n",
       "        [15, 16, 17, 18, 19]],\n",
       "\n",
       "       [[20, 21, 22, 23, 24],\n",
       "        [25, 26, 27, 28, 29],\n",
       "        [30, 31, 32, 33, 34],\n",
       "        [35, 36, 37, 38, 39]],\n",
       "\n",
       "       [[40, 41, 42, 43, 44],\n",
       "        [45, 46, 47, 48, 49],\n",
       "        [50, 51, 52, 53, 54],\n",
       "        [55, 56, 57, 58, 59]]])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "three = np.array(range(60)).reshape(3,4,5)\n",
    "three"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "2df3f063-8247-4a6c-825a-d5d078b77043",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[20, 21, 22, 23, 24],\n",
       "        [25, 26, 27, 28, 29]],\n",
       "\n",
       "       [[40, 41, 42, 43, 44],\n",
       "        [45, 46, 47, 48, 49]]])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "three[1:3,0:2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de0575d4-02f9-46e6-8151-8e335430b024",
   "metadata": {},
   "source": [
    "## axis\n",
    "- 一维数组 axis=0\n",
    "- 二维数组 横轴(第一维度)axis=1 纵轴(第二维度)axis=0\n",
    "- 三维数组 横轴(第一维度)axis=2, 纵轴(第二维度)axis=1, 深度轴(第三维度)axis=0\n",
    "- 新加的一个维度axis=0, 其他原来的维度axis增一\n",
    "- shape的第一个值是axis=0, 第二个值axis=1..."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "709e0c18-c7f3-496b-b0f6-a87593fb7b9e",
   "metadata": {},
   "source": [
    "对于多维数组的堆叠，vstack只能按轴0,hstack只能按轴1,其他维度的堆叠则可以通过concatenate函数指定轴向进行。\n",
    "堆叠的条件是两个数组除了堆叠的轴向上的元素数可以不同外，其他轴向须具有相同的形状。\n",
    "np.concatenate((a,b,c...),axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "059c3c03-246b-4185-b719-912b2defd4b4",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "## 一维数组堆叠"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "cf70c8a8-fb7c-46fb-86e3-c86907a9c91f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# vstack(), hstack()\n",
    "#vertical, horizontal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "5503d8a7-b209-40c4-8b58-66656c8f7b9c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T10:44:31.430486Z",
     "start_time": "2025-01-04T10:44:31.422455Z"
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4]\n",
      "[10 20 30 40 50]\n"
     ]
    }
   ],
   "source": [
    "a=np.array(np.arange(5))\n",
    "b=np.array([10,20,30,40,50])\n",
    "print(a)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "76d8f7a2-65c3-4689-8769-4e10e145597f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T10:44:31.491995Z",
     "start_time": "2025-01-04T10:44:31.486348Z"
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4, 10, 20, 30, 40, 50])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1=np.hstack((a,b))  #水平堆叠 np.concatenate((a,b),axis=0) \n",
    "a1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "776efa89-4057-4431-b121-19ca168eba7d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10,)"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "dd21d7d9-47f7-48a1-b99d-4963d71f27ae",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T10:44:31.593681Z",
     "start_time": "2025-01-04T10:44:31.588985Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4],\n",
       "       [10, 20, 30, 40, 50]])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1=np.vstack((a,b)) # 垂直(行)堆叠 np.concatenate((a,b),axis=0) \n",
    "a1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "1ec44287-41c3-4b11-bafd-45c63a1de970",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T10:44:31.682400Z",
     "start_time": "2025-01-04T10:44:31.670021Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 0, 10],\n",
       "        [ 1, 20],\n",
       "        [ 2, 30],\n",
       "        [ 3, 40],\n",
       "        [ 4, 50]]])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2=np.dstack((a,b)) # 深度堆叠 延第三轴(深度)堆叠数组列表\n",
    "a2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "afc403f0-5b57-46ec-81bb-ff341fbd4620",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 5, 2)"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "dd3ec990-221a-4d07-a04a-7f37198f12e6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T10:44:31.871674Z",
     "start_time": "2025-01-04T10:44:31.867521Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0, 10],\n",
       "       [ 1, 20],\n",
       "       [ 2, 30],\n",
       "       [ 3, 40],\n",
       "       [ 4, 50]])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.column_stack((a,b)) #  列堆叠"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7672f1d-9f4c-4d78-b003-ee05e4fcaef0",
   "metadata": {},
   "source": [
    "## 二维数组堆叠\n",
    "对于多维数组hstack按axis=1堆叠, vstack按axis=0堆叠"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "227359c1-690d-4d94-b938-a7943224f2da",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T11:55:14.483229Z",
     "start_time": "2025-01-04T11:55:14.478034Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([['a', 'a', 'a'],\n",
       "       ['a', 'a', 'a']], dtype='<U1')"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aa = np.full((2,3),'a')\n",
    "aa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "e9b8f83d-3c3b-4c1c-88f1-948769e805a9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T12:38:32.470550Z",
     "start_time": "2025-01-04T12:38:32.463937Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([['b', 'b', 'b'],\n",
       "       ['b', 'b', 'b']], dtype='<U1')"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bb =np.full((2,3),'b')\n",
    "bb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "b62aa47e7ce749d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T12:39:48.364909Z",
     "start_time": "2025-01-04T12:39:48.358876Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([['a', 'a', 'a'],\n",
       "       ['a', 'a', 'a'],\n",
       "       ['b', 'b', 'b'],\n",
       "       ['b', 'b', 'b']], dtype='<U1')"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vstack((aa,bb)) # 水平堆叠"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "803d25e4c0d36c95",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T12:32:08.908003Z",
     "start_time": "2025-01-04T12:32:08.899872Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([['a', 'a', 'a', 'b', 'b', 'b'],\n",
       "       ['a', 'a', 'a', 'b', 'b', 'b']], dtype='<U1')"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hstack((aa,bb))  # 垂直堆叠"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "7e34eaf90f511f21",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T12:31:00.662878Z",
     "start_time": "2025-01-04T12:31:00.657500Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([['a', 'a', 'a', 'b', 'b', 'b'],\n",
       "       ['a', 'a', 'a', 'b', 'b', 'b']], dtype='<U1')"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.column_stack((aa,bb)) # 列堆叠"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "8fe67bca49677b6f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-04T12:32:24.663335Z",
     "start_time": "2025-01-04T12:32:24.656557Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ True,  True,  True,  True,  True,  True],\n",
       "       [ True,  True,  True,  True,  True,  True]])"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.column_stack((aa,bb))==np.hstack((aa,bb))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0252bf29-606f-4c2c-881f-0206ad79617d",
   "metadata": {},
   "source": [
    "## 拆分数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "c29c40e9-fe44-4482-ad19-402b7ca364ad",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 垂直,水平,深度方向拆分数组\n",
    "# hsplit(),vsplit(),dsplit(),split()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "2c8b10ac-c514-4359-bff3-02fcad86ed8c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 2],\n",
       "       [3, 4, 5, 2],\n",
       "       [6, 7, 8, 2]])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.array([[0,1,2,2],[3,4,5,2],[6,7,8,2]])\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "5864f81e-2998-40a5-81f2-ceb5460e9eb7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[0],\n",
       "        [3],\n",
       "        [6]]),\n",
       " array([[1],\n",
       "        [4],\n",
       "        [7]]),\n",
       " array([[2],\n",
       "        [5],\n",
       "        [8]]),\n",
       " array([[2],\n",
       "        [2],\n",
       "        [2]])]"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r=np.hsplit(c,4) # 等价 np.split(c,4,axis=1) 等分4份\n",
    "type(r)  # list\n",
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "0d4c74b2-8409-4585-a8db-b91b545be4d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[0],\n",
       "        [3],\n",
       "        [6]]),\n",
       " array([[1, 2],\n",
       "        [4, 5],\n",
       "        [7, 8]]),\n",
       " array([[2],\n",
       "        [2],\n",
       "        [2]])]"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hsplit(c,(1,3)) # 第一列后和第三列后切开"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "3f801d5f-8b5b-4ed2-bbab-590037c84849",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[0, 1, 2, 2]]), array([[3, 4, 5, 2]]), array([[6, 7, 8, 2]])]"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vsplit(c,3) # 等价 np.split(c,3,axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46a92a7a-19a1-4baa-8920-24d52cd51dd6",
   "metadata": {},
   "source": [
    "## 数组属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "5c6d8a24-30c5-4520-a636-5a1d1c9b7d4d",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# shape,dtype,ndim,size,itemsize,nbytes,T,flat"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c69abb04-a3c3-43e6-a49c-9c6d2af5d4fe",
   "metadata": {},
   "source": [
    "## 改变形状和维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "3070ed59-5074-493d-8183-a2dd779a5b96",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(6,)"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# a=np.append(a,5)  # 数组增加数据\n",
    "a=np.array(range(6))\n",
    "a.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "6005834f-8282-4a1c-aea4-32d83b1db733",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [3, 4, 5]])"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape((2,3)) # reshape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "bf379180-7001-40bc-a229-3a09f2bbeab8",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4, 5]])"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.reshape(a,(1,a.size)) # 增加了一个维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "32202b54-88d6-4c96-b5b7-c9b11ee4e162",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4, 5]])"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[None,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "a31b79f6-fdcd-46e5-96d2-a4afac5b445b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0],\n",
       "       [1],\n",
       "       [2],\n",
       "       [3],\n",
       "       [4],\n",
       "       [5]])"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.expand_dims(a,axis=1) # 在1轴上增加(插入)一个维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "a2f72e9e-8483-484b-bb65-2546b5970e46",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4, 5]])"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.expand_dims(a,axis=0) # 在1轴上增加(插入)一个维度"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cd0eb6a2-84fa-4419-9bf0-ec16151dbe4d",
   "metadata": {},
   "source": [
    "## 转换数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "5e97ffee-4e02-41ab-9d2c-81a9f3fa2d59",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 使用tolist()将numpy数组转换为python列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "6da45584-789c-4e7a-8792-a6ffc348fe42",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "834fb80b-93f6-4e77-97e6-217fc974d840",
   "metadata": {},
   "outputs": [],
   "source": [
    "# astype()将数组转换为指定类型的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "84209193-0fad-4ed8-beea-1d9d694f143f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 1., 2., 3., 4., 5.])"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.astype(float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ae6d6681-38f6-4922-9146-4427b351ec16",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.8"
  },
  "toc-showmarkdowntxt": false
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
